package com.koron.dynamic.koronDataSource;

import com.zaxxer.hikari.HikariDataSource;
import org.koron.ebs.mybatis.ADOSessionImpl;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * @ClassName KrDataSourceConfig
 * @description 配置数据源信息
 * @Author zhouj
 * @Date 2021/7/15 19:21
 */
@Configuration
public class KrDataSourceConfig {

    //这里实际上是对数据源的缓存
    @Bean
    @Primary
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource primaryDataSource() {
        HikariDataSource ds =  DataSourceBuilder.create().type(HikariDataSource.class).build();
        ADOSessionImpl session = new ADOSessionImpl();
        session.removeInterceptor("_default", "com.github.pagehelper.PageInterceptor");
        session.addInterceptor("_default", "com.github.pagehelper.PageInterceptor");
        session.removeInterceptor("_default", "com.koron.common.core.interceptor.FieldInterceptor");
        session.addInterceptor("_default", "com.koron.common.core.interceptor.FieldInterceptor");

        //开启/关闭-自动转换驼峰标识
        SessionFactory.setMapUnderscoreToCamelCase(true);
        session.registeDataSourceMap("_default", ds);
        return ds;
    }

}
